﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;



namespace Crazycrash.Interface
{
    public enum AnimalTypes
    {
        DOG = 1, CAT, MONKEY, LION, PEGION
    }
    public class AnimalTypesEntity
    {
        private AnimalTypes animalTypes;

        public AnimalTypes AnimalTypes
        {
            get { return animalTypes; }
            set { animalTypes = value; }
        }
        public AnimalTypesEntity(AnimalTypes a)
        {
            animalTypes = a;
        }
    }
    public class AnimalControl
    {
        const int SIZE = 8;
        private AnimalTypesEntity[,] animals;//The animals are those animals entities in the game panel

        public AnimalTypesEntity[,] Animals
        {
            get { return animals; }
            set { animals = value; }
        }

        private int[,] tags; //The tags are to be used to show out the 
        public int[,] Tags
        {
            get { return tags; }
            set { tags = value; }
        }
        public AnimalControl()
        {
            animals = new AnimalTypesEntity[SIZE, SIZE];
            for (int j = 0; j < SIZE; j++)
                for (int i = 0; i < SIZE; i++)
                    animals[i, j] = new AnimalTypesEntity(AnimalTypes.CAT);
            tags = new int[SIZE, SIZE];
            for (int j = 0; j < SIZE; j++)
                for (int i = 0; i < SIZE; i++)
                    tags[i, j] = 0;
        }
        public void generate()
        {
            System.Random rand = new Random();
            for (int j = 0; j < SIZE; j++)
            {
                for (int i = 0; i < SIZE; i++)
                {
                    int temp = 0;
                    temp = rand.Next(1, 6);
                    AnimalTypes a = (AnimalTypes)temp;
                    animals[i, j].AnimalTypes = a;
                }
            }

            List<Point2> list = GetEliminatedPoints();
            if (list.Count == 0)
            {
                resetTag();
                return;
            }
            else
            {
                for (int i = 0; i < list.Count; i++)
                {
                    int temp = rand.Next(1, 6);
                    animals[list[i].X, list[i].Y].AnimalTypes = (AnimalTypes)temp;
                }
            }
            list = GetEliminatedPoints();
            resetTag();
            if (list.Count == 0)
            {

                return;
            }
            else
            {
                for (int i = 0; i < list.Count; i++)
                {
                    int temp = rand.Next(1, 6);
                    animals[list[i].X, list[i].Y].AnimalTypes = (AnimalTypes)temp;
                }
            }
            list = GetEliminatedPoints();
            resetTag();
            if (list.Count == 0)
            {
                return;
            }
            else
            {
                int temp = rand.Next(0, 10);
                Console.WriteLine("{0} ", temp);
                AnimalTypes[,] Enity = null;
                switch (temp)
                {
                    case 0: Enity = new AnimalTypes[,]{
                     {AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.DOG},
                     {AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.CAT},
                     {AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.LION},
                     {AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.PEGION},
                     {AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.LION},
                     {AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.LION},
                     {AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.MONKEY},
                     {AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.PEGION},
                    };
                        break;
                    case 1: Enity = new AnimalTypes[,]{
                    {AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.DOG},
                    {AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.PEGION},
                    {AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.LION},
                    {AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.CAT},
                    {AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.CAT,AnimalTypes.LION},
                    {AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.LION},
                    {AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.MONKEY},
                    {AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.CAT}
                    };
                        break;
                    case 2: Enity = new AnimalTypes[,]{
                    {AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.MONKEY},
                    {AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.CAT},
                    {AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.LION},
                    {AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.PEGION},
                    {AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.LION},
                    {AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.LION},
                    {AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.DOG},
                    {AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.PEGION},
                    };
                        break;
                    case 3: Enity = new AnimalTypes[,]{
                    {AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.CAT},
                    {AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.PEGION},
                    {AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.LION},
                    {AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.DOG},
                    {AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.DOG,AnimalTypes.LION},
                    {AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.LION},
                    {AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.MONKEY},
                    {AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.DOG}
                };
                        break;
                    case 4: Enity = new AnimalTypes[,]{
                    {AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.LION},
                    {AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.MONKEY},
                    {AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.PEGION,AnimalTypes.CAT},
                    {AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.DOG},
                    {AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.PEGION,AnimalTypes.LION},
                    {AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.PEGION},
                    {AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.LION},
                    {AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.CAT}
                };
                        break;
                    case 5: Enity = new AnimalTypes[,]{
                    {AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.LION},
                    {AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.MONKEY},
                    {AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.PEGION},
                    {AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.DOG},
                    {AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.LION},
                    {AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.CAT},
                    {AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.CAT,AnimalTypes.LION},
                    {AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.PEGION}
                };
                        break;
                    case 6: Enity = new AnimalTypes[,]{
                    {AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.LION},
                    {AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.DOG},
                    {AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.PEGION,AnimalTypes.CAT},
                    {AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.MONKEY},
                    {AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.PEGION,AnimalTypes.LION},
                    {AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.PEGION},
                    {AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.LION},
                    {AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.CAT}
                };
                        break;
                    case 7: Enity = new AnimalTypes[,]{
                    {AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.LION},
                    {AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.MONKEY},
                    {AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.PEGION},
                    {AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.CAT},
                    {AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.LION},
                    {AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.DOG},
                    {AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.DOG,AnimalTypes.LION},
                    {AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.PEGION}
                };
                        break;
                    case 8: Enity = new AnimalTypes[,]{
                    {AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.PEGION},
                    {AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.MONKEY,AnimalTypes.LION},
                    {AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.PEGION,AnimalTypes.MONKEY},
                    {AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.DOG},
                    {AnimalTypes.CAT,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.DOG},
                    {AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.MONKEY},
                    {AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.CAT},
                    {AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.MONKEY}
                };
                        break;
                    case 9: Enity = new AnimalTypes[,]{
                    {AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.PEGION},
                    {AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.CAT},
                    {AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.LION},
                    {AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.DOG},
                    {AnimalTypes.MONKEY,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.DOG},
                    {AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.LION},
                    {AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.MONKEY},
                    {AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.LION}
                };
                        break;
                    default: Enity = new AnimalTypes[,]{
                    {AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.PEGION},
                    {AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.LION,AnimalTypes.CAT},
                    {AnimalTypes.MONKEY,AnimalTypes.PEGION,AnimalTypes.MONKEY,AnimalTypes.DOG,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.LION},
                    {AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.LION,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.DOG},
                    {AnimalTypes.MONKEY,AnimalTypes.MONKEY,AnimalTypes.CAT,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.DOG},
                    {AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.LION,AnimalTypes.LION},
                    {AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.DOG,AnimalTypes.LION,AnimalTypes.CAT,AnimalTypes.MONKEY},
                    {AnimalTypes.LION,AnimalTypes.DOG,AnimalTypes.PEGION,AnimalTypes.PEGION,AnimalTypes.CAT,AnimalTypes.DOG,AnimalTypes.MONKEY,AnimalTypes.LION}
                };
                        break;
                }
                for (int j = 0; j < SIZE; j++)
                    for (int i = 0; i < SIZE; i++)
                        animals[i, j].AnimalTypes = Enity[i, j];
            }

        }
        public void resetTag()
        {
            for (int j = 0; j < SIZE; j++)
                for (int i = 0; i < SIZE; i++)
                    tags[i, j] = 0;
        }
        public void print()
        {
            for (int j = 0; j < SIZE; j++)
            {
                for (int i = 0; i < SIZE; i++)
                    Console.Write(" {0}  ", animals[i, j].AnimalTypes);
                Console.WriteLine();
            }
        }
        public void printTag()
        {
            for (int j = 0; j < SIZE; j++)
            {
                for (int i = 0; i < SIZE; i++)
                    Console.Write("  {0}   ", tags[i, j]);
                Console.WriteLine();
            }
        }
        public List<Point2> GetEliminatedPoints()
        {
            List<List<Point2>> all = new List<List<Point2>>(); // This list is going to include all the legal AnimalTypes line.
            for (AnimalTypes i = AnimalTypes.DOG; i <= AnimalTypes.PEGION; i++)//For all the Animals,organize them into an array
            {
                List<Point2> AnimalsOfOneType = getPoints(i);
                process(AnimalsOfOneType, ref all);
            }
            List<Point2> result = new List<Point2>();
            foreach (List<Point2> item in all)
            {
                foreach (Point2 p in item)
                {
                    if (!result.Contains(p))
                    {
                        result.Add(p);
                        tags[p.X, p.Y] = -1;//The tags are changed for the points to be eliminated, based on that all the tags are 0 before.
                    }
                }
            }
            return result;
        }
        public void Regenerate()
        {
            Random r = new Random();
            for (int i = 0; i < SIZE; i++)
            {
                int temptag = 0;
                for (int j = SIZE - 1; j >= 0; j--)
                {
                    if (tags[i, j] == -1)
                        temptag++;
                    else
                    {
                        tags[i, j] = temptag;
                    }
                }
                for (int j = SIZE - 1; j >= 0; j--)
                {
                    int temp = tags[i, j];
                    if (temp > 0)
                        animals[i, j + temp].AnimalTypes = animals[i, j].AnimalTypes;
                }
                int temp2 = tags[i, 0];
                if (temp2 > 0)
                {
                    for (int j = 0; j < temp2; j++)
                    {
                        int newPic = r.Next(1, 6);
                        animals[i, j].AnimalTypes = (AnimalTypes)newPic;
                    }
                    continue;
                }
                if (temp2 < 0)
                {
                    for (int j = 0; j < SIZE && tags[i, j] < 0; j++)
                    {
                        int newPic = r.Next(1, 6);
                        animals[i, j].AnimalTypes = (AnimalTypes)newPic;
                    }
                }
            }
            resetTag();
        }
        //This function is going to find Animals of the same type
        private List<Point2> getPoints(AnimalTypes a)
        {
            List<Point2> oneType = new List<Point2>();
            for (int j = 0; j < SIZE; j++)
                for (int i = 0; i < SIZE; i++)
                {
                    if (Animals[i, j].AnimalTypes == a)
                    {
                        Point2 p = new Point2(i, j);
                        oneType.Add(p);
                    }
                }

            return oneType;
        }
        //This function will check for all the AnimalTypes Places and include those can be eliminated.
        public void process(List<Point2> l, ref List<List<Point2>> all)
        {

            /*The tags here to check if there is still need to check the left-right or the up-down 
             * direction of a certain Point2. -1 stands for both are needed.0 stands for only the left-right direction
             of this Point2 has been checked. 1 stands for only the up-down direction has been checked.
             2 stands for both have been checked and there's no need to check the Point2 */

            List<int> tags = new List<int>();

            for (int i = 0; i < l.Count; i++)
            {
                tags.Add(-1);
            }

            //This function is going to examine all the 
            for (int i = 0; i < l.Count; i++)
            {
                if (tags[i] == 2) continue;
                switch (tags[i])
                {
                    case -1: checkLR(l, ref all, ref tags, i);
                        checkUD(l, ref all, ref tags, i);
                        break;
                    case 0: checkUD(l, ref all, ref tags, i);
                        break;
                    case 1: checkLR(l, ref all, ref tags, i);
                        break;
                    default: break;
                }
            }
        }
        //check the left-right direction for legal points.
        private void checkLR(List<Point2> l, ref List<List<Point2>> all, ref List<int> tags, int i)
        {
            tags[i] = increaseTagLR(tags[i]);
            int tempX = l[i].X;
            int j = tempX + 1;
            int tempY = l[i].Y;
            int k = i + 1;
            for (; k < l.Count && j < SIZE && l.Contains(new Point2(j, tempY)); j++, k++)
            {
                tags[k] = increaseTagLR(tags[k]);
            }
            if (j <= tempX + 2)
                return;
            else
            {
                List<Point2> legalSeq = new List<Point2>();
                for (int s = i; s < k; s++)
                {
                    legalSeq.Add(l[s]);
                }
                all.Add(legalSeq);
            }
        }
        private void checkUD(List<Point2> l, ref List<List<Point2>> all, ref List<int> tags, int i)
        {
            tags[i] = increaseTagUD(tags[i]);
            int tempx = l[i].X;
            int tempy = l[i].Y;
            int j = tempy + 1;
            List<Point2> tmp = new List<Point2>();
            tmp.Add(l[i]);
            for (; j < SIZE && l.Contains(new Point2(tempx, j)); j++)
            {
                int temp = findIndex(ref l, tempx, j);
                tags[temp] = increaseTagUD(tags[temp]);
                tmp.Add(l[temp]);
            }
            if (j <= tempy + 2)
                return;
            all.Add(tmp);
        }
        private int increaseTagLR(int i)
        {
            if (i == -1) return 0;
            if (i == 1) return 2;
            return -2;
        }
        private int increaseTagUD(int i)
        {
            if (i == -1) return 1;
            if (i == 0) return 2;
            return -2;
        }
        private int findIndex(ref List<Point2> l, int x, int y)
        {
            for (int i = 0; i < l.Count; i++)
                if (l[i].X == x && l[i].Y == y)
                    return i;
            return -1;
        }
        public Point2 HelpJudge()
        {
            for (AnimalTypes i = AnimalTypes.DOG; i <= AnimalTypes.PEGION; i++)
            {
                List<Point2> l = getPoints(i);
                Point2 temp = judge1(l);
                if (temp != null) return temp;
                temp = judge2(l);
                if (temp != null) return temp;
            }
            return null;
        }
        private Point2 judge1(List<Point2> l)
        {
            for (int i = 0; i < l.Count - 1; i++)
            {
                int tempXi = l[i].X;
                int tempYi = l[i].Y;
                if (l[i + 1].Y == tempYi && l[i + 1].X == tempXi + 2)
                {
                    int j = findIndex(ref l, tempXi + 1, tempYi - 1);
                    if (j != -1) return l[j];
                    j = findIndex(ref l, tempXi + 1, tempYi + 1);
                    if (j != -1) return l[j];
                }
                int temp = findIndex(ref l, tempXi, tempYi + 2);
                if (temp != -1)
                {
                    int j = findIndex(ref l, tempXi + 1, tempYi + 1);
                    if (j != -1) return l[j];
                    j = findIndex(ref l, tempXi - 1, tempYi + 1);
                    if (j != -1) return l[j];
                }
            }
            return null;
        }
        private Point2 judge2(List<Point2> l)
        {
            for (int i = 0; i < l.Count - 1; i++)
            {
                int tempXi = l[i].X;
                int tempYi = l[i].Y;
                if (l[i + 1].X == tempXi + 1 && l[i + 1].Y == tempYi)
                {
                    int temp1 = findIndex(ref l, tempXi - 1, tempYi - 1);
                    if (temp1 != -1) return l[temp1];
                    temp1 = findIndex(ref l, tempXi - 1, tempYi + 1);
                    if (temp1 != -1) return l[temp1];
                    temp1 = findIndex(ref l, tempXi + 2, tempYi - 1);
                    if (temp1 != -1) return l[temp1];
                    temp1 = findIndex(ref l, tempXi + 2, tempYi + 1);
                    if (temp1 != -1) return l[temp1];
                }
                int temp = findIndex(ref l, tempXi, tempYi + 1);
                if (temp != -1)
                {
                    int temp1 = findIndex(ref l, tempXi - 1, tempYi - 1);
                    if (temp1 != -1) return l[temp1];
                    temp1 = findIndex(ref l, tempXi - 1, tempYi + 2);
                    if (temp1 != -1) return l[temp1];
                    temp1 = findIndex(ref l, tempXi + 1, tempYi - 1);
                    if (temp1 != -1) return l[temp1];
                    temp1 = findIndex(ref l, tempXi + 1, tempYi + 2);
                    if (temp1 != -1) return l[temp1];
                }
            }
            return null;
        }
        public void swap(Point2 x, Point2 y)
        {
            AnimalTypes temp = animals[x.X, x.Y].AnimalTypes;
            animals[x.X, x.Y].AnimalTypes = animals[y.X, y.Y].AnimalTypes;
            animals[y.X, y.Y].AnimalTypes = temp;
        }
    }
    public class Point2
    {
        private int x;

        public int X
        {
            get { return x; }
            set { x = value; }
        }
        private int y;
        public int Y
        {
            get { return y; }
            set { y = value; }
        }
        public Point2(int x1, int x2)
        {
            x = x1;
            y = x2;
        }
        public override bool Equals(Object p)
        {
            return (((Point2)p).x == x && ((Point2)p).y == y);
        }
    }
}



